package com.example.springbootpreviewurls.common;

import com.aspose.cad.CodePages;
import com.aspose.cad.Image;
import com.aspose.cad.Color;
import com.aspose.cad.InterruptionTokenSource;
import com.aspose.cad.LoadOptions;
import com.aspose.cad.fileformats.cad.CadDrawTypeMode;
import com.aspose.cad.fileformats.cad.CadImage;
import com.aspose.cad.fileformats.tiff.enums.TiffExpectedFormat;
import com.aspose.cad.imageoptions.*;

//import java.awt.*;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
/**
*
*/
public class cadToPdf {
    public Boolean cadToPdf(String inputFilePath, String outputFilePath) {
        // 创建CAD延时处理器
        final InterruptionTokenSource source = new InterruptionTokenSource();
        // 创建SVG、PDF和TIFF选项
        final SvgOptions svgOptions = new SvgOptions();
        final PdfOptions pdfOptions = new PdfOptions();
        final TiffOptions tiffOptions = new TiffOptions(TiffExpectedFormat.TiffJpegRgb);

        //获取输出文件格式
        String cadPreviewType = outputFilePath.substring(outputFilePath.lastIndexOf(".") + 1);

        // 获取输出文件路径中的目录
        int index = outputFilePath.lastIndexOf("/");
        System.out.println("index:" + index);
        String folder = outputFilePath.substring(0, index);
        File path = new File(folder);
        // 如果目录不存在，则创建新的目录
        if (!path.exists()) {
            path.mkdirs();
        }
        // 创建输出文件
        File outputFile = new File(outputFilePath);

        try {
            // 加载CAD文件并设置指定的编码
            LoadOptions opts = new LoadOptions();
            opts.setSpecifiedEncoding(CodePages.SimpChinese);
            final Image  objImage = Image.load(inputFilePath, opts);
            CadImage cadImage = (CadImage) objImage;
            cadImage.setDefaultFont("仿宋");
            try {
                // 设置矢量化质量
                RasterizationQuality rasterizationQuality = new RasterizationQuality();
                rasterizationQuality.setArc(RasterizationQualityValue.High);
                rasterizationQuality.setHatch(RasterizationQualityValue.High);
                rasterizationQuality.setText(RasterizationQualityValue.High);
                rasterizationQuality.setOle(RasterizationQualityValue.High);
                rasterizationQuality.setObjectsPrecision(RasterizationQualityValue.High);
                rasterizationQuality.setTextThicknessNormalization(true);

                // 设置CAD矢量化选项
                CadRasterizationOptions cadRasterizationOptions = new CadRasterizationOptions();
                cadRasterizationOptions.setBackgroundColor(Color.getWhite());
                cadRasterizationOptions.setPageWidth(cadImage.getWidth());
                cadRasterizationOptions.setPageHeight(cadImage.getHeight());
                cadRasterizationOptions.setUnitType(cadImage.getUnitType());
                cadRasterizationOptions.setAutomaticLayoutsScaling(false);
                cadRasterizationOptions.setNoScaling(false);
                cadRasterizationOptions.setQuality(rasterizationQuality);
                cadRasterizationOptions.setDrawType(CadDrawTypeMode.UseObjectColor);
                cadRasterizationOptions.setExportAllLayoutContent(true);
                cadRasterizationOptions.setVisibilityMode(VisibilityMode.AsScreen);

                // 根据cadPreviewType选择不同的输出格式选项
                switch (cadPreviewType) {
                    case "svg":
                        svgOptions.setVectorRasterizationOptions(cadRasterizationOptions);
                        svgOptions.setInterruptionToken(source.getToken());
                        break;
                    case "pdf":
                        pdfOptions.setVectorRasterizationOptions(cadRasterizationOptions);
                        pdfOptions.setInterruptionToken(source.getToken());
                        break;
                    case "tif":
                        tiffOptions.setVectorRasterizationOptions(cadRasterizationOptions);
                        tiffOptions.setInterruptionToken(source.getToken());
                        break;
                }

                // 保存CAD文件为指定格式
                try (OutputStream stream = new FileOutputStream(outputFile)) {
                    switch (cadPreviewType) {
                        case "svg":
                            cadImage.save(stream, svgOptions);
                            break;
                        case "pdf":
                            cadImage.save(stream, pdfOptions);
                            break;
                        case "tif":
                            cadImage.save(stream, tiffOptions);
                            break;
                    }
                } catch (IOException e) {
                    System.out.println(e);
                    return false;
                } finally {
                    // 释放资源并中断任务
                    cadImage.dispose();
                    source.interrupt();
                    source.dispose();
                }
                return true;

            } finally {
                // 释放资源
                source.dispose();
                cadImage.dispose();
            }
        }catch (Exception e){
            e.printStackTrace();
            return false;
        } finally {
            // 释放资源
            source.dispose();
        }
    }
    public static void main(String[] args) {
        String inputFilePath = "C:/Users/zcw/Desktop/colorwh.dwg";
        String outputFilePath = "C:/Users/zcw/Desktop/colorwh.pdf";
        cadToPdf cadToPdf = new cadToPdf();
        Boolean result = cadToPdf.cadToPdf(inputFilePath, outputFilePath);
        System.out.println(result);
    }
}



